﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace Room
{
    public class RCAIServerMonster : RCAIBase
    {
        public RCAIServerMonster(RoomCharactor owner)
            : base(owner)
        {
            type = AIType.ServerMonster;
            InitFSM();
        }

        public float height;
        public float hpOffest;

        #region FSM
        public int curState;
        private Dictionary<int, ServerMonsterAIStateBase> allStates = new Dictionary<int, ServerMonsterAIStateBase>();
        public override void Tick()
        {
            base.Tick();
            allStates[curState].Tick();
            if(owner.IsMoving())
            {
                owner.PlayAnim("run");
            }
            else
            {
                owner.PlayAnim("stand");
            }

        }

        private void InitFSM()
        {
            curState = ServerMonsterAIState.Normal;

            allStates[ServerMonsterAIState.None] = new ServerMonsterAIStateBase();
            allStates[ServerMonsterAIState.Normal] = new ServerMonsterAIStateNormal();
            allStates[ServerMonsterAIState.Battle] = new ServerMonsterAIStateBattle();
            allStates[ServerMonsterAIState.Death] = new ServerMonsterAIStateDeath();


            foreach (var state in allStates)
            {
                state.Value.controller = this;
            }
        }

        public override void SwitchState(int newState)
        {
            if (curState != newState)
            {
                if (allStates.ContainsKey(newState))
                {
                    allStates[curState].Leave();
                    curState = newState;
                    allStates[newState].Enter();
                }
                else
                {
                    Debug.LogError("[ERROR] server give state id:" + (int)newState);
                }
            }
        }
        #endregion

        public void Init(SideMonster monster)
        {
            var x = monster.currentPoint.x;
            //var y = monster.currentPoint.y;
            var z = monster.currentPoint.z;

            //Debug.Log("[ServerMonster]  uuid:" + monster.uuid + " pos: x=" + x + ",y=" + y + ",z=" + z);

            var moveTo = new Vector3(x, owner.transform.position.y, z);
            
            if (owner.transform.position.x != x ||
                owner.transform.position.z != z)
            {
                owner.StopTo(moveTo); 
            }
        }

        //GameObject obj;
        public override void SetHp(float hp)
        {
            /*
            if (obj == null)
            {
                obj = GameObjectPoolManager.Instance.Spawn(RoomField.bloodBarPrefab);

                obj.transform.SetParent(owner.transform);
                obj.transform.localScale = Vector3.one;
                obj.transform.localPosition = Vector3.zero + new Vector3(0, height+ hpOffest, 0);
                obj.SetActive(true);

            }

            var kit = obj.GetComponent<BloodBarNumControl>();
            kit.Init();
            kit.SetNowBarValue(hp, 9999f);
            */
        }

        protected void UnloadHp()
        {
            //if(obj!=null)
            //{
                //GameObjectPoolManager.Instance.Unspawn(obj);
            //}
        }

        public override void DoStop()
        {
            base.DoStop();
            UnloadHp();
        }

    }
}